package ils.domain.acquisition.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class SellerFactoryImpl implements SellerFactory
{
    private SessionFactory sessionFactory;
    private static final String GET_SELLERS = "select seller from Seller as seller where seller.id in (:idList)";
    @Override
    public Seller getSeller(int id)
    {
        Session session = sessionFactory.getCurrentSession();
        return (Seller)session.load(Seller.class, id);
    }
    @Override
    public void createSeller(Seller seller)
    {
        Session session = sessionFactory.getCurrentSession();
        session.save(seller);
    }
    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }
    @Override
    public Map<Integer, Seller> getSellers(List<Integer> sellerIds)
    {
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery(GET_SELLERS);
        query.setParameterList("idList", sellerIds);
        List<Seller> sellers = query.list();
        Map<Integer, Seller> sellersMap = new HashMap<Integer, Seller>();
        for(Seller seller : sellers)
        {
            sellersMap.put(seller.getId(), seller);
        }
        return sellersMap;
    }
    
}
